<?php
/**
 * Created by PhpStorm.
 * UsersF: Mloong
 * Date: 2018/12/6
 * Time: 9:43
 */

namespace app\login\validate;


use app\admin\model\Account;
use app\api\model\Record;
use app\lib\exception\ParameterException;
use think\facade\Session;

class NamePwdValidate extends BaseValidate
{
    protected $rule = [
        'username'  =>  'require|checkName',
        'password' =>  'require',
        'captcha|验证码'=>'require|captcha'
    ];
    # 自定义对账号和密码的校验
    protected function checkName($value, $rule='', $data='', $field='')
    {
        $model = new Account();
        $username = $model->where('name','=',$value)
            ->find();
        if(!$username){
            return false;
        }
        $user_info = $username->toArray();
        if($user_info['status'] == 0){
            throw new ParameterException([
                'msg' => '账号已被停用',
                'errorCode' => '4001'
            ]);
        }
        # 对比从数据库查询出来的密码，如果一样则返回 true
        $pwd = ($user_info['pwd']);
        $md5_pwd = md5($data['password']);
        if(!($pwd == $md5_pwd)){
            throw new ParameterException([
                'msg' => '密码错误',
                'errorCode' => '4002'
            ]);
        }
        return true;
    }
}